/*
 * IIC.c
 *
 *  Created on: Jul 11, 2021
 *      Author: 本地账户
 */

#include "IIC.h"
#include "delay.h"


void IIC1_Init(void)
{
    GPIO_InitTypeDef GPIO_Initure;

    __HAL_RCC_GPIOB_CLK_ENABLE();


    GPIO_Initure.Pin=GPIO_PIN_8|GPIO_PIN_9;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;
    GPIO_Initure.Pull=GPIO_PULLUP;
    GPIO_Initure.Speed=GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);

    IIC1_SDA=1;
    IIC1_SCL=1;
}

void IIC2_Init(void)
{
    GPIO_InitTypeDef GPIO_Initure;

    __HAL_RCC_GPIOB_CLK_ENABLE();


    GPIO_Initure.Pin=GPIO_PIN_5|GPIO_PIN_6;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;
    GPIO_Initure.Pull=GPIO_PULLUP;
    GPIO_Initure.Speed=GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);

    IIC2_SDA=1;
    IIC2_SCL=1;
}

void IIC3_Init(void)
{
    GPIO_InitTypeDef GPIO_Initure;

    __HAL_RCC_GPIOD_CLK_ENABLE();


    GPIO_Initure.Pin=GPIO_PIN_5|GPIO_PIN_6;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;
    GPIO_Initure.Pull=GPIO_PULLUP;
    GPIO_Initure.Speed=GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOD,&GPIO_Initure);

    IIC3_SDA=1;
    IIC3_SCL=1;
}


void IIC1_Start(void)
{
	SDA1_OUT();
	IIC1_SDA=1;
	IIC1_SCL=1;
	delay_us(4);
 	IIC1_SDA=0;//START:when CLK is high,DATA change form high to low
	delay_us(4);
	IIC1_SCL=0;
}

void IIC2_Start(void)
{
	SDA2_OUT();
	IIC2_SDA=1;
	IIC2_SCL=1;
	delay_us(4);
 	IIC2_SDA=0;//START:when CLK is high,DATA change form high to low
	delay_us(4);
	IIC2_SCL=0;
}

void IIC3_Start(void)
{
	SDA3_OUT();
	IIC3_SDA=1;
	IIC3_SCL=1;
	delay_us(4);
 	IIC3_SDA=0;//START:when CLK is high,DATA change form high to low
	delay_us(4);
	IIC3_SCL=0;
}

void IIC1_Stop(void)
{
	SDA1_OUT();
	IIC1_SCL=0;
	IIC1_SDA=0;//STOP:when CLK is high DATA change form low to high
 	delay_us(4);
	IIC1_SCL=1;
	IIC1_SDA=1;
	delay_us(4);
}

void IIC2_Stop(void)
{
	SDA2_OUT();
	IIC2_SCL=0;
	IIC2_SDA=0;//STOP:when CLK is high DATA change form low to high
 	delay_us(4);
	IIC2_SCL=1;
	IIC2_SDA=1;
	delay_us(4);
}

void IIC3_Stop(void)
{
	SDA3_OUT();
	IIC3_SCL=0;
	IIC3_SDA=0;//STOP:when CLK is high DATA change form low to high
 	delay_us(4);
	IIC3_SCL=1;
	IIC3_SDA=1;
	delay_us(4);
}

u8 IIC1_Wait_Ack(void)
{
	u8 ucErrTime=0;
	SDA1_IN();
	IIC1_SDA=1;delay_us(1);
	IIC1_SCL=1;delay_us(1);
	while(READ1_SDA)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			IIC1_Stop();
			return 1;
		}
	}
	IIC1_SCL=0;
	return 0;
}

u8 IIC2_Wait_Ack(void)
{
	u8 ucErrTime=0;
	SDA2_IN();
	IIC2_SDA=1;delay_us(1);
	IIC2_SCL=1;delay_us(1);
	while(READ2_SDA)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			IIC2_Stop();
			return 1;
		}
	}
	IIC2_SCL=0;
	return 0;
}

u8 IIC3_Wait_Ack(void)
{
	u8 ucErrTime=0;
	SDA3_IN();
	IIC3_SDA=1;delay_us(1);
	IIC3_SCL=1;delay_us(1);
	while(READ3_SDA)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			IIC3_Stop();
			return 1;
		}
	}
	IIC3_SCL=0;
	return 0;
}

void IIC1_Ack(void)
{
	IIC1_SCL=0;
	SDA1_OUT();
	IIC1_SDA=0;
	delay_us(2);
	IIC1_SCL=1;
	delay_us(2);
	IIC1_SCL=0;
}

void IIC2_Ack(void)
{
	IIC2_SCL=0;
	SDA2_OUT();
	IIC2_SDA=0;
	delay_us(2);
	IIC2_SCL=1;
	delay_us(2);
	IIC2_SCL=0;
}

void IIC3_Ack(void)
{
	IIC3_SCL=0;
	SDA3_OUT();
	IIC3_SDA=0;
	delay_us(2);
	IIC3_SCL=1;
	delay_us(2);
	IIC3_SCL=0;
}

void IIC1_NAck(void)
{
	IIC1_SCL=0;
	SDA1_OUT();
	IIC1_SDA=1;
	delay_us(2);
	IIC1_SCL=1;
	delay_us(2);
	IIC1_SCL=0;
}

void IIC2_NAck(void)
{
	IIC2_SCL=0;
	SDA2_OUT();
	IIC2_SDA=1;
	delay_us(2);
	IIC2_SCL=1;
	delay_us(2);
	IIC2_SCL=0;
}

void IIC3_NAck(void)
{
	IIC3_SCL=0;
	SDA3_OUT();
	IIC3_SDA=1;
	delay_us(2);
	IIC3_SCL=1;
	delay_us(2);
	IIC3_SCL=0;
}

void IIC1_Send_Byte(u8 txd)
{
    u8 t;
	SDA1_OUT();
    IIC1_SCL=0;
    for(t=0;t<8;t++)
    {
        IIC1_SDA=(txd&0x80)>>7;
        txd<<=1;
		delay_us(2);
		IIC1_SCL=1;
		delay_us(2);
		IIC1_SCL=0;
		delay_us(2);
    }
}

void IIC2_Send_Byte(u8 txd)
{
    u8 t;
	SDA2_OUT();
    IIC2_SCL=0;
    for(t=0;t<8;t++)
    {
        IIC2_SDA=(txd&0x80)>>7;
        txd<<=1;
		delay_us(2);
		IIC2_SCL=1;
		delay_us(2);
		IIC2_SCL=0;
		delay_us(2);
    }
}

void IIC3_Send_Byte(u8 txd)
{
    u8 t;
	SDA3_OUT();
    IIC3_SCL=0;
    for(t=0;t<8;t++)
    {
        IIC3_SDA=(txd&0x80)>>7;
        txd<<=1;
		delay_us(2);
		IIC3_SCL=1;
		delay_us(2);
		IIC3_SCL=0;
		delay_us(2);
    }
}

u8 IIC1_Read_Byte(unsigned char ack)
{
	unsigned char i,receive=0;
	SDA1_IN();
    for(i=0;i<8;i++ )
	{
        IIC1_SCL=0;
        delay_us(2);
		IIC1_SCL=1;
        receive<<=1;
        if(READ1_SDA)receive++;
		delay_us(1);
    }
    if (!ack)
        IIC1_NAck();
    else
        IIC1_Ack();
    return receive;
}

u8 IIC2_Read_Byte(void)
{
	unsigned char i,receive=0;
	SDA2_IN();
    for(i=0;i<8;i++ )
	{
        IIC2_SCL=0;
        delay_us(2);
		IIC2_SCL=1;
        receive<<=1;
        if(READ2_SDA)receive++;
		delay_us(1);
    }
	IIC2_SCL=0;
	SDA2_OUT();//sda线输出
	return receive;

//    if (!ack)
//    IIC2_NAck();
//    else
//    IIC2_Ack();
//    return receive;
}

u8 IIC3_Read_Byte(unsigned char ack)
{
	unsigned char i,receive=0;
	SDA3_IN();
    for(i=0;i<8;i++ )
	{
        IIC3_SCL=0;
        delay_us(2);
		IIC3_SCL=1;
        receive<<=1;
        if(READ3_SDA)receive++;
		delay_us(1);
    }
    if (!ack)
        IIC3_NAck();
    else
        IIC3_Ack();
    return receive;
}





